/**
 * Created by WYH on 17/6/22.
 */
angular.module('companyInfo', []).component(
    'companyInfo',
    {
        templateUrl: 'component/approve/approveList/company_info.template.html',
        controller: [
            '$rootScope','$scope','Api', 'CompanyModel', 'ElePosition','Alert',
            function ($rootScope ,$scope, Api, CompanyModel, ElePosition,Alert) {

                $scope.approveButtonClick = approveButtonClick;
                $scope.companyInfo = $rootScope.companyInfo;
                $rootScope.$watch('companyInfo',function(newCompanyInfo){
                    $scope.companyInfo = new CompanyModel(newCompanyInfo);
                });
                $scope.isCanEdit = false;

                $scope.showPictureGroup = 1;

                setupPictureGroup();


                $scope.getContactNameStyle = function () {
                    return {
                        "left": ($("#baseContactName").width() + 8) + 'px'
                    };
                };


                $scope.showCompanyEdit = function (number) {
                    $("#proWriteContentBox").css('top', (ElePosition.getScrollTop() + 20) + 'px');
                    $scope.$parent.writeStep = number;
                };

                function approveButtonClick()
                {
                    Api.approveCompany($scope.companyInfo.id,function (result) {
                        window.location.reload();
                    });
                }


                function setupPictureGroup() {
                    $scope.$watch('showPictureGroup', function (newGroup) {
                        if ($scope.companyInfo && Object.keys($scope.companyInfo).length != 0 && Object.keys($scope.companyInfo.pictures).length!=0) {
                            if (newGroup > (($scope.companyInfo.pictures.length+1) / 2 )) {
                                Alert.alertBox('没有更多图片了');
                                $scope.showPictureGroup--;
                            } else if (newGroup < 1) {
                                Alert.alertBox('已经到第一页了');
                                $scope.showPictureGroup++;
                            } else {
                                changeShowPicture(newGroup);
                            }
                        }

                    });
                }

                function changeShowPicture(group) {
                    var backBox = document.getElementById('profileBack');
                    var pictureBox = document.getElementsByClassName('pro-company-pictures')[0];
                    pictureBox.style['margin-left'] = '-' + backBox.offsetWidth * (group - 1) + 'px';
                }

            }
        ]
    }
);